
<template>
  <div class="accountList">
    <div class="searchBox">
      <el-form :model="searchForm" ref="searchFormRef">
        <el-row>
          <el-form-item prop="name">
            <div class="item">
              <span v-html="'字典名称'" class="nameSpan"></span>
              <el-input
                size="mini"
                v-model="searchForm.name"
                placeholder="请输入字典名称"
              ></el-input>
            </div>
          </el-form-item>

          <el-form-item prop="key">
            <div class="item">
              <span v-html="'字典类型'" class="nameSpan"></span>
              <el-input
                size="mini"
                v-model="searchForm.key"
                placeholder="请输入字典名称"
              ></el-input>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="searchClick"
              icon="el-icon-search"
              type="primary"
              size="mini"
              >查询</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetClick"
              >重置</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
    </div>

    <div class="topBtn">
      <el-button
        v-has="'system:dict:add'"
        size="mini"
        @click="addDictClick"
        type="success"
        plain
        icon="el-icon-plus"
        >新增字典</el-button
      >
    </div>

    <div class="tableBox" ref="tableBoxRef">
      <el-table
      
        v-loading="tableLoading"
        :header-cell-style="{
          background: '#F8F8F8',
          color: '#333',
          fontSize: '16px',
          fontWeight: '700',
        }"
        border
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="name"
          align="center"
          label="字典名称"
           
        >
        </el-table-column>
        <el-table-column
          prop="key"
          align="center"
          label="字典类型"
           
        >
        <template slot-scope="scope">
             <div @click="getdictDetail(scope.row)" class="active">{{scope.row.key}}</div>
        </template>
        </el-table-column>
        <el-table-column
          prop="remark"
          align="center"
          label="备注"
           
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          align="center"
          label="创建时间"
           
        >
        <template slot-scope="scope">{{
            scope.row.createTime | formatDate
          }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit-outline"
              size="mini"
              type="text"
              v-has="'system:dict:edit'"
              @click="editClick(scope.row,scope.$index)"
              >编辑</el-button
            >
            <el-button
              icon="el-icon-delete"
              size="mini"
              type="text"
              @click="delClick(scope.row)"
              v-has="'system:dict:delete'"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

        <el-card class="footer" v-if="total > 0">
         <div>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[15, 30, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      </el-card>
    </div>

    <!-- 新增/修改权限 -->
    <el-dialog :close-on-click-modal="false" 
      :title="title"
      :visible.sync="addEditDialogVisible"
      width="500px"
      @open="addEditDialogOpen"
      class="addEditAccountDialog"
    >
      <div class="content">
        <el-form
          :model="addEditForm"
          :rules="addEditRules"
          ref="addEditFormRef"
        >
          <el-form-item prop="name" label="字典名称：">
            <div class="item">
              <el-input
                size="mini"
                v-model="addEditForm.name"
                placeholder="请输入字典名称"
                maxlength="16"
              >
              </el-input>
            </div>
          </el-form-item>
             <el-form-item prop="key" label="字典类型：">
            <div class="item">
              <el-input
              :disabled="title=='编辑字典'"
                size="mini"
                v-model="addEditForm.key"
                placeholder="请输入字典类型"
                  maxlength="32"
              >
              </el-input>
            </div>
          </el-form-item>
         <el-form-item prop="remark" label="备注：">
            <div class="item">
              <el-input
                type="textarea"
                size="mini"
                v-model="addEditForm.remark"
                placeholder="请输入备注"
                :rows="2"
              >
              </el-input>
            </div>
          </el-form-item>

          <el-form-item class="btns">
            <el-button size="mini" @click="addEditDialogVisible = false"
              >取 消</el-button
            >
            <el-button
              v-clickForbidden
              size="mini"
              type="primary"
              @click="addEditDialogSaveClick"
              >确 定</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      screenHeight: document.documentElement.clientHeight,
      typeOptions: [],
      searchForm: {
        name:"",
        key:"",
      },
      stautsOptions: [
        { label: "正常", value: 1 },
        { label: "禁用", value: 2 },
      ],
      tableLoading: false,
      tableData: [],
      pageNum: 1,
      pageSize: 15,
      total: 0,
      addEditDialogVisible: false,
      addEditForm: {
       name:"",
       key:"",
       remark:""
      },
      addEditRules: {
        name: [
          { required: true, message: "字典名称不能为空", trigger: "blur" },
        ],
        key: [
          { required: true, message: "字典类型不能为空", trigger: "blur" },
        ],
      },
      title: "新增字典",
      editId: "",
    };
  },
  computed: {},

  created() {
    this.getDictionaryList();
  },
  methods: {
    getdictDetail(row){
         this.$router.push({
        path: "/systemManagement/dictionaryDetail",
        query: { id: row.id },
      });
    },
    resetClick() {
      this.$refs.searchFormRef.resetFields();
      this.pageNum = 1;
      this.getDictionaryList();
    },
    searchClick() {
      this.pageNum = 1;
      this.getDictionaryList();
    },


    /**
     * 获取标准库列表
     * @param {*}
     */
    async getDictionaryList() {
      this.tableLoading = true;
      const res = await this.$api.dictList({
        size: this.pageSize,
        page: this.pageNum,
        condition: {
          ...this.searchForm,
        },
      });
      this.total = res.data.result.total;
      this.tableData = res.data.result.records;
      this.tableLoading = false;
    },

    /**
     * 新增字典
     * @param {*}
     */
    addDictClick() {
      this.title = "新增字典";
      this.addEditDialogVisible = true;
    },
    /**
     * 编辑按钮
     * @param {*} row
     */
    async editClick(row,index) {
      this.title = "编辑字典";
      this.addEditDialogVisible = true;
      this.editId = row.id;
      this.$nextTick(() => {
        this.addEditForm = JSON.parse(JSON.stringify(this.tableData[index]))
      });
    },
    /**
     * 删除按钮
     * @param {*} row
     */
    delClick(row) {
      this.$confirm("确定删除当前字典吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api.delDict(row.id).then((res) => {
            if (res.data.code == "00000") {
              this.$message.success("删除成功");
              this.getDictionaryList();
            }
          });
        })
        .catch(() => {});
    },

    /**
     * 切换页面大小
     * @param {*} val
     */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getDictionaryList();
    },

    /**
     * 页码切换
     * @param {*} val
     */
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.getDictionaryList();
    },

    /**
     * 打开新增和修改的回调
     * @param {*}
     */
    addEditDialogOpen() {
      if (this.$refs.addEditFormRef) this.$refs.addEditFormRef.resetFields();
    },

    /**
     * 新增编辑保存按钮
     * @param {*}
     */
    addEditDialogSaveClick() {
      this.$refs.addEditFormRef.validate(async (boolean) => {
        if (boolean) {
          let data = {
            id: this.editId,
           ...this.addEditForm
          };
          if (this.title == "新增字典") {
            data.id=''
            const res = await this.$api.addDict(data);
            if (res.data.code == "00000") {
              this.$message.success("新增成功");
              this.getDictionaryList();
              this.addEditDialogVisible = false;
            }
          } else {
            const res = await this.$api.editDict(data);
            if (res.data.code == "00000") {
              this.$message.success("修改成功");
              this.getDictionaryList();
              this.addEditDialogVisible = false;
            }
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.accountList {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
  .searchBox {
    .el-row {
      display: flex;
      flex-wrap: wrap;
      .el-form-item {
        margin-bottom: 0;
      }
      .item {
        display: flex;
        align-items: center;
        margin-right: 20px;
        .nameSpan {
          padding-right: 10px;
          box-sizing: border-box;
          white-space: nowrap;
        }
        .el-input {
          width: 162px;
        }
      }
    }
  }
   .topBtn {
    margin: 10px 0;
text-align: right;
  }
        .active {
      color: #409eff;
      cursor: pointer;
      text-decoration: underline;
    }

  .addEditAccountDialog {
    .el-form-item {
      .el-select {
        width: 100%;
      }
    }
    .btns {
      text-align: right;
      margin-bottom: 0;
    }
  }
}
/deep/.el-button--success.is-plain {
  color: #fff;
  border-color: #409eff;
  background-color: #409eff;
}
/deep/.el-form-item {
  margin-bottom: 12px;
}
</style>